package com.test.GenericDemo;

import org.junit.Test;

import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.URL;
import java.util.Arrays;
import java.util.Scanner;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/06/05 9:15
 **/
public class GeneImpl<T> implements Gene<T> {
    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    /*public GeneImpl() {
        ParameterizedType pt = (ParameterizedType) (this.getClass().getGenericSuperclass());
        Class aClass = (Class) pt.getActualTypeArguments()[0];
        System.out.println(aClass);
    }*/

    @Override
    public T show() {
        System.out.println(t);
        return t;

    }

    public static void main(String[] args) throws Exception {
        GeneImpl<String> gene = new GeneImpl<String>();
        gene.setT("h呵呵呵呵");
        gene.show();
        Class c = gene.getClass();
        Type t = c.getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            System.out.println("in if");
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            System.out.println(Arrays.toString(p));
        }
    }

    @Test
    public void test66() throws Exception {
        InetAddress[] allByName = InetAddress.getAllByName("3ds.guide");
        for (InetAddress inetAddress : allByName) {
            System.out.println(inetAddress);
        }
    }

    @Test
    public void test56() throws Exception {
        URL http = new URL("http", "www.baidu.com", 80, "/index.htm");
        InputStream inputStream = http.openStream();
        Scanner scanner = new Scanner(inputStream);
        scanner.useDelimiter("\n");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
        }
    }
}
